/************************************************************************
 *File Name: isOrder.c
 *Author: wky
 *Created Time: 2023年06月21日 星期三 10时55分54秒
 *Describtion:客户端对于用户输入的命令进行判断 
 ************************************************************************/
#include "client.h"

int isOrder(char*sentence,order_t* ord )
{   
    if(strcmp("\n" , sentence)== 0){
        ord->num = OTHRE; 
        return 0;
    }
    char *token1,*token2,*token3;
    token1 = strtok(sentence," \n");               

    
    if(strcmp("exit",token1) == 0)
    {
        ord->num = EXIT;
        return 1;
    }
    else if(strcmp("cd",token1) == 0) //cd filename
    {   
        ord->num = CD;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
            strcpy(ord->opt, token2);
            token3 = strtok(NULL," \n");
            if(token3 != NULL)
            {
                printf("cd: 参数太多\n");
                return -1;
            }
        }

       // printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else if(strcmp("ls",token1) == 0) // ls 
    {   
        ord->num = LS;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
                printf("ls: 参数太多\n");
                return -1;
        }

        //printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else if(strcmp("puts",token1) == 0)//puts fiilename
    {   
        ord->num = PUTS;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
            strcpy(ord->opt, token2);
            token3 = strtok(NULL," \n");
            if(token3 != NULL)
            {
                printf("puts: 参数太多\n");
                return -1;
            }
        }
        else
        {
            printf("puts: 缺少操作对象\n");
        }

       // printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else if(strcmp("gets",token1) == 0) // gets filename 
    {   
        ord->num = GETS;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
            strcpy(ord->opt, token2);
            token3 = strtok(NULL," \n");
            if(token3 != NULL)
            {
                printf("gets: 参数太多\n");
                return -1;
            }
        }
        else
        {
            printf("gets: 缺少操作对象\n");
        }

        //printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else if(strcmp("remove",token1) == 0) //remove filename
    {   
        ord->num = REMOVE;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
            strcpy(ord->opt, token2);
            token3 = strtok(NULL," \n");
            if(token3 != NULL)
            {
                printf("remove: 参数太多\n");
                return -1;
            }
        }
        else
        {
            printf("remove: 缺少操作对象\n");
        }

       // printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else if(strcmp("pwd",token1) == 0) //pwd
    {   
        ord->num = PWD;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
                printf("pwd: 参数太多\n");
                return -1;
        }

       // printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else if(strcmp("mkdir",token1) == 0) //mkdir filename
    {   
        ord->num = MKDIR;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
            strcpy(ord->opt, token2);
            token3 = strtok(NULL," \n");
            if(token3 != NULL)
            {
                printf("mkdir: 参数太多\n");
                return -1;
            }
        }
        else
        {
            printf("mkdir: 缺少操作对象\n");
            return -1;
        }


       // printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else if(strcmp("rmdir",token1) == 0) //rmdir filename
    {   
        ord->num = RMDIR;
        token2 = strtok(NULL," \n");
        if(token2 != NULL)
        {
            strcpy(ord->opt, token2);
            token3 = strtok(NULL," \n");
            if(token3 != NULL)
            {
                printf("rmdir: 参数太多\n");
                return -1;
            }
        }
        else
        {
            printf("rmdir: 缺少操作对象\n");
            return -1;
        }


        //printf("num = %d, opt = %s\n",ord->num,ord->opt);
        return 1;  
    }
    else{
        printf("%s: 未找到命令\n",token1);
        return -1;
    }

    return 0;
} 


//int main(){
//
//    char sentence[512];
//    order_t ord;
//    bzero(sentence,sizeof(sentence));
//    read(STDIN_FILENO, sentence,sizeof(sentence));
//    isOrder( sentence, &ord);
//
//    return 0;
//}
